package DataBaseTest;

import com.auqaoasis.model.DataBase;
import com.auqaoasis.model.field.Field;
import com.auqaoasis.model.field.FieldType;
import com.auqaoasis.model.field.IntField;
import com.auqaoasis.model.page.heap.HeapPage;
import com.auqaoasis.model.page.heap.HeapPageID;
import com.auqaoasis.model.page.PageID;
import com.auqaoasis.model.row.Row;
import com.auqaoasis.model.table.tableFile.HeapTableFile;
import com.auqaoasis.model.table.Table;
import com.auqaoasis.model.table.TableDesc;
import com.auqaoasis.model.table.tableFile.TableFile;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @Author Auqaoasis
 * @Date 2022/3/30 - 16:42
 * @Description: 测试插入数据
 */
public class DataBaseTest2 {
    public DataBase dataBase;
    String path = "D:\\代码文件\\面试项目\\simple-db\\src\\test\\java\\DataBaseTest\\insertRow_test.txt";

    @Before
    public void initDataBase() {
        DataBase dataBase = DataBase.getInstance();
        ;

        HeapTableFile heapTableFile = new HeapTableFile(new File(path));
//        String fileName = UUID.randomUUID().toString();
//        HeapTableFile heapTableFile = new HeapTableFile(new File(fileName));

        List<TableDesc.Attribute> attributes = Arrays.asList(new TableDesc.Attribute("age", FieldType.INT));
//        List<TableDesc.Attribute> attributes = Arrays.asList(new TableDesc.Attribute("age", FieldType.INT), new TableDesc.Attribute("birthday", FieldType.LONG));
        TableDesc tableDesc = new TableDesc(attributes);

        Table t_person_table = new Table("t_person", heapTableFile, tableDesc);

        dataBase.addTable(t_person_table);
        this.dataBase = dataBase;
    }
    //
    // @Test
    // public void writePageToDisk() {
    //     DataBase dataBase = new DataBase();
    //     // 创建数据库文件
    //     // 路径修改
    //     DbTableFile dbTableFile = new DbTableFile(new File("disk_test.txt"));
    //     List<TableDesc.Attribute> attributes = Arrays.asList(new TableDesc.Attribute("f1", FieldType.INT));
    //     TableDesc tableDesc = new TableDesc(attributes);
    //     // tableDesc
    //     dataBase.addTable(dbTableFile, "t_person", tableDesc);
    //     HeapPage page = new HeapPage(0, new byte[]{});
    //     // 数据写入file
    //     dbTableFile.writePageToDisk(page);
    // }

    @Test
    public void insertRow() throws IOException {
        Table tablePerson = this.dataBase.getTableByName("t_person");
        Row row = new Row(tablePerson.getTableDesc());
        tablePerson.insertRow(row);
    }

    /**
     * 一页4096字节，表t_person有一个int型字段，占用4字节，slot状态占用1字节
     * 4096/(4+1) = 819(向下取整)，即每页可容纳819行
     */
    @Test
    public void testCalculateMaxSlotNum() throws IOException {
        Table tablePerson = this.dataBase.getTableByName("t_person");
        HeapPage heapPage = new HeapPage(new HeapPageID(tablePerson.getTableId(), 0), HeapPage.createEmptyPageData());
        int i = heapPage.calculateMaxSlotNum(tablePerson.getTableDesc());
        System.out.println(i);
        Assert.assertEquals(819, i);
    }

    /**
     * t_person 表只有一个int类型字段
     */

    @Test
    public void insertRowTest() throws IOException {
        Table tablePerson = this.dataBase.getTableByName("t_person");
        Row row = new Row(tablePerson.getTableDesc());

        // 第1页
        for (int i = 0; i < 819; i++) {
            row.setField(0, new IntField(i));
            tablePerson.insertRow(row);
            int existPageCount = tablePerson.getHeapTableFile().getExistPageCount();
            Assert.assertEquals(1, existPageCount);
        }

        // 第2页
        for (int i = 0; i < 819; i++) {
            row.setField(0, new IntField(i));
            tablePerson.insertRow(row);
            int existPageCount = tablePerson.getHeapTableFile().getExistPageCount();
            Assert.assertEquals(2, existPageCount);
        }
        for (int i = 0; i < 2; i++) {
            row.setField(0, new IntField(i));
            tablePerson.insertRow(row);
            int existPageCount = tablePerson.getHeapTableFile().getExistPageCount();
            Assert.assertEquals(3, existPageCount);
        }
        //说明找到空的slot
        row.setField(0, new IntField(1000));
        tablePerson.insertRow(row);
        int existPageCount = tablePerson.getHeapTableFile().getExistPageCount();
        Assert.assertEquals(3, existPageCount);
    }

    //测试反序列化后能不能看到数据
    @Test
    public void watchInsert() throws IOException {
        Table tablePerson = this.dataBase.getTableByName("t_person");
        TableFile tableFile = tablePerson.getHeapTableFile();
        int existPageCount = tableFile.getExistPageCount();
        for(int i =0;i<=existPageCount;i++){
            PageID pageID = new HeapPageID(tablePerson.getTableId(), i);
            HeapPage heapPage = (HeapPage) tableFile.readPageFromDisk(pageID);
            Row[] rows = heapPage.getRows();
            for (Row row : rows) {
                if(row!=null){
                    for (Field field : row.getFields()) {
                        System.out.print(field.toString() + " ");
                    }
                }
            }

            System.out.println("\n---------------------------页分割线---------------------------");
        }

    }
}
